package com.wms.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.entity.Admin;
import com.wms.entity.Blog;
import com.wms.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RequestMapping("/admin/blog")
@RestController("adminBlogController")
public class BlogController extends BaseController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/list")
    public Object list(Page<Blog> page, String title) {
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        queryWrapper.select(Blog.class, blog -> !blog.getColumn().equals("content"));
        IPage<Blog> iPage = blogService.page(page, queryWrapper);
        return success(iPage);
    }


    @RequestMapping("/save")
    public Object save(Blog blog) {
        if (StringUtils.isEmpty(blog.getTitle()) || StringUtils.isEmpty(blog.getDescription()) || StringUtils.isEmpty(blog.getContent()) || blog.getState() == null){
            return error("参数不完整");
        }

        blogService.save(blog);
        return success();
    }

    @RequestMapping("/detail")
    public Object edit(Long id) {
        if (id == null){
            return error("参数不完整");
        }

        Blog blog = blogService.getById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", blog.getId());
        map.put("title", blog.getTitle());
        map.put("state", blog.getState());
        map.put("musicId", blog.getMusicId());
        map.put("content", blog.getContent());
        map.put("markdown", blog.getMarkdown());
        map.put("description", blog.getDescription());
        map.put("createDate", blog.getCreateDate());
        return success(map);
    }


    @RequestMapping("/update")
    public Object update(Blog blog) {
        if (blog.getId() == null){
            return error("参数不完整");
        }

        blogService.updateById(blog);
        return success();
    }


}
